package com.hengxinguotong.zhihuichengjian.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.Company;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/7/13.
 */

public class CompanyListAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<Company> companyList;
    private int type;//适配器类型：1代表公司； 2代表班组
    private int select = -1;

    public CompanyListAdapter(List<Company> companyList, Context context, int type) {
        inflater = LayoutInflater.from(context);
        this.companyList = companyList;
        this.type = type;
    }

    public Company getSelect() {
        if (select == -1) {
            return null;
        }
        return companyList.get(select);
    }

    public void setSelect(int select) {
        this.select = select;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return companyList.size();
    }

    @Override
    public Object getItem(int position) {
        return companyList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null || convertView.getTag() == null) {
            convertView = inflater
                    .inflate(R.layout.item_company_list, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Company company = companyList.get(position);
        if(type == 1){
            switch (company.getType()){//类型 1:专业工程分包公司；2:劳务分包公司; 3:租赁公司
                case 1:
                    holder.companyType.setText("专业工程分包公司");
                    break;
                case 2:
                    holder.companyType.setText("劳务分包公司");
                    break;
                case 3:
                    holder.companyType.setText("租赁公司");
                    break;
                case 4:
                    holder.companyType.setText("建设单位");
                    break;
                case 5:
                    holder.companyType.setText("施工单位");
                    break;
                case 6:
                    holder.companyType.setText("监理单位");
                    break;
            }
            holder.companyName.setText(company.getName());
        }else if(type == 2){
            holder.companyType.setText(company.getName());
            holder.companyName.setText(company.getUserName() + "   " + company.getMobile());
        }
        if (position == select) {
            holder.checkbox.setImageResource(R.mipmap.circle_choice_ligt);
        } else {
            holder.checkbox.setImageResource(R.mipmap.circle_choice);
        }
        return convertView;
    }

    static class ViewHolder{
        @BindView(R.id.company_type)
        HXTextView companyType;
        @BindView(R.id.company_name)
        HXTextView companyName;
        @BindView(R.id.checkbox)
        ImageView checkbox;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
